import {getDeviceList,getDeviceTypes} from  "../riverPlayer/node_modules/@/api/request"
import markIcon from "./mark.png"
import infoBox from '../riverPlayer/node_modules/@/plugins/toolsManage/infoWindow/infoWindow'
import {BillboardGraphics,LabelGraphics,Color,HeightReference,Cartesian3,Cartesian2,NearFarScalar,VerticalOrigin,ScreenSpaceEventHandler,ScreenSpaceEventType,Cesium,defined,EntityCollection} from "cesium"
const devicesMark = function(viewer,pram){
    var self = this
    this._viewer = viewer
    this.entityCollection = new EntityCollection()
    this.infoWindow = new infoBox(viewer)
    //点击回调
    this.clickCallback = pram.clickCallback || function(evt){
        console.log(evt)
    }
    this.markIcon = {
        "GPS":markIcon,
        "雷达":markIcon,
        "角反":markIcon
    }
    this.typeMap = {

    }
    getDeviceTypes().then(res=>{
         res.data.data.forEach(type=>{
            self.typeMap[type.id] = type.name
        })
    })
    this._bind_evt()
}
devicesMark.prototype = {
    getData:function(id){
        var self = this
        this.clear()
        getDeviceList({id:id}).then(res=>{
            try{
                console.log(res.data.data)
                res.data.data.forEach(geologyE => {
                    var lon = parseFloat(geologyE.cpost.split(" ")[1])
                    var lat = parseFloat(geologyE.cpost.split(" ")[0])
                    var height = parseFloat(geologyE.cpost.split(" ")[2]) || 0
                    if(lon > 0  && lon <180 && lat>0 && lat<90){
                        var geologyId = geologyE.id || "0001"
                        var entity = {
                            position:Cartesian3.fromDegrees(lon,lat,height),
                            billboard:new BillboardGraphics({
                                image:self.markIcon[self.typeMap[geologyE.deviceType]],
                                // disableDepthTestDistance:Number.POSITIVE_INFINITY,
                                // heightReference:HeightReference.CLAMP_TO_GROUND,
                                pixelOffset:new Cartesian2(0.0, -18.0),
                                scale:0.3
                            }),
                            label:new LabelGraphics({
                                text:geologyE.name,
                                font : '12px sans-serif',
                                fillColor :new Color(1, 1, 1, 1),
                                outlineWidth : 2,
                                showBackground:true,
                                backgroundColor:new Color(0.1, 0.1, 0.1, 0.8),
                                verticalOrigin : VerticalOrigin.BOTTOM,
                                pixelOffset : new Cartesian2(0, 20),
                                scaleByDistance: new NearFarScalar(1800000, 1, 10000000, 0),
                                disableDepthTestDistance:2000000,
                                // heightReference:HeightReference.CLAMP_TO_GROUND
                            }),
                            device_click:function(){
                                var tempPoup = `<div class="infolist"><span >设备名称:${geologyE.name}</span>`
                                self.infoWindow.show(geologyE.name,tempPoup)
                            },
                            key:"deviceMark",
                            id:geologyId,
                       }
                       self.add(entity)
                       
                    }else{
                        console.log("坐标不正确")
                    }
                });
                
            }catch{

            }
           
        })
    },
    _bind_evt:function(){
        var self  = this
        this.handler = new ScreenSpaceEventHandler(this._viewer.scene.canvas);
        this.handler.setInputAction(function (movement) {
            var pick = self._viewer.scene.pick(movement.position);
            if (defined(pick) && pick.id) {
                if(pick.id.device_click){
                    pick.id.device_click()
                }
            }else{
                self.infoWindow.close()
            }
        },ScreenSpaceEventType.LEFT_CLICK)
    },
    clear:function(){
        this.entityCollection.values.forEach((entity,index)=>{
            this._viewer.entities.remove(entity)
            if(index == this.entityCollection.values.length-1){
                this.entityCollection.removeAll()
            }
        })
    },
    add:function(entity){
        this.entityCollection.add(entity)
        this._viewer.entities.add(entity)
    },
    hide:function(){

    },
    show:function(){

    },
    addTo:function(){

    },
    goto:function(id){
        var currentEntity = this._viewer.entities.getById(id)
        if(currentEntity){
            this._viewer.flyTo(currentEntity)
        }
        
    }
}
export default devicesMark